package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientMapper;
import com.leyou.auth.service.ClientService;
import com.leyou.commons.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientInfo> implements ClientService {
   @Autowired
   private PasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;
    /**
     * 获取秘钥
     * @param clientId
     * @param secret
     * @return
     */
    @Override
    public String getSecretKey(String clientId, String secret) {
        ClientInfo clientInfo = query().eq("client_id", clientId).one();
        if(null==clientInfo){
            throw new LyException(401,"客户端的信息有误=="+clientId+" 不存在");
        }
        if(!passwordEncoder.matches(secret,clientInfo.getSecret())){
            throw new LyException(401,"客户端的信息有误=="+secret+"不正确");
        }

        return key;
    }
}
